package com.link.base.base.questionsurvey.service;

import com.link.base.base.message.push.service.PushService;
import com.link.base.base.mvg.dao.mybatis.question.mapper.QuestionUserMvgMapper;
import com.link.base.base.mvg.model.Mvg;
import com.link.base.base.questionsurvey.dao.mybatis.mapper.QuestionMapper;
import com.link.base.base.questionsurvey.model.*;
import com.link.base.base.user.model.User;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.dao.KeyGenerateDao;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import com.link.core.cllog.LCLogger;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author unknown
 * @date unknown
 * @comments 问卷操作服务
 */
@Component
public class QuestionServiceImpl extends BasicServiceImpl<Question> implements QuestionService {
    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private KeyGenerateDao keyGenerateDao;
    @Resource
    private QuestionUserMvgMapper questionUserMvgMapper;
    @Resource
    private PushService pushService;

    @Override
    public BasicMapper<Question> getBasicMapper() throws Exception {
        return questionMapper;
    }

    @Override
    public List<User> queryUserFillInPage(User entity) throws Exception {
        return questionMapper.queryUserFillInPage(entity);
    }

    @Override
    public List<Question> queryQuestionSurveyPage(Question entity) throws Exception {
        return questionMapper.queryQuestionSurveyPage(entity);
    }

    @Override
    public List<Question> queryQuestionByUserPage(Question entity, Long userId) throws Exception {
        if (null == userId) {
            throw new ServiceException("PUBLIC-016");
        }
        entity.setAttr1(userId.toString());
        return questionMapper.queryQuestionByUserPage(entity);
    }

    @Override
    public Question queOneQuestionDetailInfo(Long id) throws Exception {
        if (id == null) {
            throw new ServiceException("PUBLIC-007");
        }
        Question qtParam = new Question();
        qtParam.setId(id);
        QuestionItem qti = new QuestionItem();
        qti.setQuestionId(id);
        qti.setPageFlag(false);
//        qti.setOrderByClause("seq " + Sorter.DIR_ASC);
        List<QuestionItem> itemList = questionMapper.queryQuestionItemPage(qti);
        for (QuestionItem q : itemList) {
            QuestionOption qo = new QuestionOption();
            qo.setItemId(q.getId());
            qo.setPageFlag(false);
//            qo.setOrderByClause("seq " + Sorter.DIR_ASC);
            List<QuestionOption> optionList = questionMapper.queryQuestionOptionPage(qo);
            List<QuestionItemReport> QuestionItemReportList = questionMapper.queryquestionItemReport(q);
            q.setAnsList(QuestionItemReportList);
            q.setOptionList(optionList);
            ;
        }
        Question question = questionMapper.queryQuestionById(qtParam);
        question.setItemList(itemList);
        return question;
    }


//    public List<Question> queryQuestionListWithAnsReport(Question qu) throws Exception {
//        List<Question> list = questionMapper.queryQuestionListOne(qu);
//
//        // 查询问卷列表
//        for (Question l : list) {
//            QuestionItem qi = new QuestionItem();
//            qi.setQuestionId(l.getId());
//            List<QuestionItem> itemList = questionMapper.queryQuestionItemPage(qi);
//            for (QuestionItem q : itemList) {
//                QuestionOption qo = new QuestionOption();
//                qo.setItemId(q.getId());
//                List<QuestionOption> optionList = questionMapper.queryQuestionOptionPage(qo);
//                List<QuestionItemReport> QuestionItemReportList = questionMapper.queryquestionItemReport(q);
//                System.out.println(q.getId());
//                q.setAnsList(QuestionItemReportList);
//                q.setOptionList(optionList);
//                ;
//            }
//            l.setItemList(itemList);
//        }
//
//        return list;
//    }


    /**
     * 查询问卷列表总记录数
     */
    public int queryCountQuestion(Question qu) throws Exception {
        return questionMapper.queryCountQuestion(qu);
    }

    /**
     * 查询问卷明细
     */
    @Override
    public Question queryQuestionItem(Question qu) throws Exception {
        Question question = questionMapper.queryQuestionItem(qu);
        return question;
    }

    /**
     * 增加问卷
     *
     *
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = BasicServiceException.class)
    public void questionTitleInsert(Question question) throws Exception {
        try {
            Long questionId = question.getId();
            if (null == questionId) {
                questionId = keyGenerateDao.keyGenerate();
                question.setId(questionId);
            }
            questionMapper.questionTitleInsert(question);
            List<QuestionItem> QstItemList = question.getItemList();
            if (QstItemList != null && QstItemList.size() > 0) {
                for (QuestionItem qstItem : QstItemList) {
                    System.out.println(qstItem);
                    qstItem.setId(keyGenerateDao.keyGenerate());
                    qstItem.setQuestionId(questionId);
                    questionMapper.questionItemInsert(qstItem);
                    Long Id1 = qstItem.getId();
                    List<QuestionOption> QstOptionList = qstItem.getOptionList();
                    if (QstOptionList != null && QstOptionList.size() > 0) {
                        for (QuestionOption o : QstOptionList) {
                            System.out.println(o);
                            o.setId(keyGenerateDao.keyGenerate());
                            o.setItemId(Id1);
                            questionMapper.questionOptionInsert(o);
                        }
                    }
                }
            }
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new RuntimeException(e.getMessage());
        }
        pushToUser(question);
    }

    /**
     * 推送消息
     */
    private void pushToUser(Question entity) {
        try {
            //发送通知
            Long id = entity.getId();
            if (null == id) {
                return;
            }
            Mvg mvgParam = new Mvg();
            mvgParam.setPageFlag(false);
            mvgParam.setMvgParentId(id);
            List<User> userList = (List<User>) (Object) questionUserMvgMapper.queryRightListPage(mvgParam);
            for (int i = 0; userList != null && i < userList.size(); i++) {
                User user = userList.get(i);
                Long userId = user.getId();
                //推送消息
                pushService.pushInfoRightAway("", "问卷填写：" + entity.getQuestionName() + "-" + entity.getCreated(),
                    userId, "order", entity.getId(), "Y", "Y");
            }
        } catch (Exception e) {
            LCLogger.withException(e);
        }
    }

    @Override
    @Transactional(rollbackFor = BasicServiceException.class)
    public void answerInsert(QuestionAnswer qu) throws Exception {
        boolean anonymousFlag = false;
        try {
            Long id1 = keyGenerateDao.keyGenerate();
            try {
                CoreUser user = UserUtil.getUser();
                qu.setEmpId(user.getId());
            } catch (Exception e) {
                LCLogger.withException(e);
                qu.setEmpId(0L);
                anonymousFlag = true;
            }
            qu.setId(id1);
            if (anonymousFlag) {
                qu.setAnonymousFlag("Y");
            }
            questionMapper.answerInsert(qu);
            if (qu.getList() != null) {
                for (QuestionAnwserItem l : qu.getList()) {
                    Long id2 = keyGenerateDao.keyGenerate();
                    l.setId(id2);
                    l.setAnswerId(id1);
                    if (anonymousFlag) {
                        l.setAnonymousFlag("Y");
                    }
                    questionMapper.answerItemInsert(l);
                    if (l.getList() != null) {
                        for (QuestionAnswerDetail o : l.getList()) {
                            Long id3 = keyGenerateDao.keyGenerate();
                            o.setId(id3);
                            o.setAnswerQuestionItemId(id2);
                            if (anonymousFlag) {
                                o.setAnonymousFlag("Y");
                            }
                            questionMapper.answerItemDetailInsert(o);
                        }
                    }
                }
            }
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new RuntimeException(e.getMessage());
        }
    }

}
